<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- structure.qdoc -->
  <title>Structure of a QML Document | Qt QML 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtqml-index.html">Qt QML</a></td><td >
			QML文档的结构</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtqml-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level1"><a href="#imports"><span lang="zh-cn">导入</span></a></li>
<li class="level1"><a href="#the-root-object-declaration">根对象声明</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QML文档的结构</h1>
<span class="subtitle"></span>
<!-- $$$qtqml-documents-structure.html-description -->
<div class="descr"> <a name="details"></a>
<p>QML文档是一个自包含的QML源代码，由两部分组成:</p>
<ul>
<li>导入语句</li>
<li>单个根对象声明</li>
</ul>
<p>按照惯例，一个空行将导入与对象层次结构定义分开。</p>
<p>QML文档总是用UTF-8格式编码。</p>
<a name="imports"></a>
<h2 id="imports"><span lang="zh-cn">导入</span></h2>
<p>
文档必须导入必要的模块或类型名称空间，以使引擎能够加载文档中引用的QML对象类型。默认情况下，文档可以访问通过同一目录中的.qml文件定义的任何QML对象类型;如果文档需要引用任何其他对象类型，它必须导入已注册这些类型的类型名称空间。</p>
<p>与C或C++不同，QML没有在将文档表示到<a href="qqmlengine.html">QML引擎</a>之前修改文档的预处理器。导入语句不会在文档中复制和添加代码，而是指示QML引擎如何解析文档中找到的类型引用。QML文档中出现的任何类型引用——比如Rectangle和ListView——包括<a href="qtqml-javascript-expressions.html">JavaScript块</a>或<a href="qtqml-syntax-propertybinding.html">属性绑定</a>中生成的类型引用，都完全基于导入语句进行解析。至少要有一个导入语句，比如导入<code>QtQuick 
2.0</code>。</p>
<p>有关QML导入的详细信息，请参阅<a href="qtqml-syntax-imports.html">QML语法-导入语句</a>文档。</p>
<h2 id="the-root-object-declaration">根对象声明</h2>
<p>QML文档描述了可以实例化的对象的层次结构。每个对象定义都有一定的结构;它有类型，可以有id和对象名，可以有属性，可以有方法，可以有信号和信号处理程序。</p>
<p>QML文件必须只包含<b>单个根对象定义</b>。下面是无效的，并将产生一个错误:</p>
<pre class="cpp">

  <span class="comment">// MyQmlFile.qml</span>
  import <span class="type">QtQuick</span> <span class="number">2.0</span>

  Rectangle { width: <span class="number">200</span>; height: <span class="number">200</span>; color: <span class="string">&quot;red&quot;</span> }
  Rectangle { width: <span class="number">200</span>; height: <span class="number">200</span>; color: <span class="string">&quot;blue&quot;</span> }    <span class="comment">// invalid!</span>

</pre>
<p>这是因为. QML文件自动定义了QML类型，该类型封装了单个QML对象定义。这将在<a href="qtqml-documents-definetypes.html">文档作为QML对象类型定义</a>中进一步讨论。</p>
</div>
<!-- @@@qtqml-documents-structure.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
